<template>
  <div class="login-container">
    <div class="login-container-con">
      <!-- <iframe frameborder="no" scrolling="auto" src="http://192.168.2.147:8080?appName=OTM系统&theme=#f00" /> -->
      <!-- <iframe frameborder="no" scrolling="auto" src="http://192.168.10.180:8080?appName=OTM系统&theme=#f00" /> -->
      <iframe frameborder="no" scrolling="auto" src="http://120.46.143.201:9090/?appName=逸品礼券后台管理系统&theme=#f00" />
    </div>
  </div>
</template>

<script>
import { verify } from '@/api/user'

import { mixpanelTracker } from '../../utils'

export default {
  name: 'Login',
  data() {
    return {
      tag: true,
      time1: 30,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入正确密码', trigger: 'blur' }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    /**
    * 监听login iframe登录
    */
    window.addEventListener('message', e => {
      const { data: { type, data }} = e
      console.log(data, type)
      if (type === 'onLoginSuccess') {
        this.loading = true
        console.log(this.$store, 'this.$store')
        this.$store.dispatch('user/login', data).then(() => {
          this.$router.push({ path: '/', query: this.otherQuery })
          this.loading = false
          mixpanelTracker('portal.login.success', {
            desc: '登录页-登录成功',
            data: this.loginForm
          })
        })
          .catch(() => {
            this.loading = false
          })
      }
    })
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          console.log(this.$store, 'this.$store')
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: '/', query: this.otherQuery })
            this.loading = false
            mixpanelTracker('portal.login.success', {
              desc: '登录页-登录成功',
              data: this.loginForm
            })
          })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    verificationCodeClick() {
      this.tag = false
      verify({ username: this.loginForm.username }).then(res => {
        console.log(res)
        let time = 30
        setInterval(() => {
          time--
          if (time > 0) {
            this.time1 = time
          } else {
            this.tag = true
          }
        }, 1000)
      })
        .catch(() => {
          this.tag = true
        })
    },
    linkTo(type) {
      mixpanelTracker(`portal.login.${type}`, {
        desc: `登录页-${type}`
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */

$bg:#fff;
$light_gray:#4E4E4E;
$cursor: #4E4E4E;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container-con {
  margin: 200px auto 0;
  width: 460px;
  height: 420px;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  iframe {
    width: 100%;
    height: 100%;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #EBEBEB;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#1363DF;
$dark_gray: #4E4E4E;
$light_gray:#4E4E4E;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    // width: ;
    max-width: 100%;
    border-radius: 10px;
    padding: 60px 100px;
    margin: 200px 30% 0;
    box-shadow: 0px 22px 28px 16px rgba(140, 35, 0, 0.14);
    overflow: hidden;
    background-color: #fff;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 4px;
    bottom: 10px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
